package com.j1ay.forum.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.j1ay.forum.dao.mapper.AnswerMapper;
import com.j1ay.forum.dao.mapper.QuestionMapper;
import com.j1ay.forum.model.pojo.po.Answer;
import com.j1ay.forum.model.pojo.po.Check;
import com.j1ay.forum.model.pojo.po.Question;
import com.j1ay.forum.model.pojo.vo.DataVo;
import com.j1ay.forum.model.pojo.vo.Result;
import com.j1ay.forum.service.DataService;
import com.sun.org.apache.xpath.internal.objects.XNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @Author J1ay
 * @Date 2021/12/26 15:38
 * @Description
 */

@Service
public class DataServiceImpl implements DataService {


    @Autowired(required = false)
    private QuestionMapper questionMapper;

    @Autowired(required = false)
    private AnswerMapper answerMapper;

    /**
     * 获取问题相关数据
     * @return
     */
    @Override
    public Result getAskCount() {
        DataVo dataVo = new DataVo();
        LambdaQueryWrapper<Question> queryWrapper = new LambdaQueryWrapper<>();
        // 问题总数
        dataVo.setTotals(questionMapper.selectCount(queryWrapper));
        // 点赞问题总数
        int totalPraise = questionMapper.findAskPraiseSum();
        dataVo.setPraiseTotals(totalPraise);
        dataVo.setAvgPraise(totalPraise / dataVo.getTotals());
        // 未审核的问题数
        queryWrapper.eq(Question::getStatus, 0);
        dataVo.setNoCheckCount(questionMapper.selectCount(queryWrapper));
        // 今日提问数
        queryWrapper.clear();
        queryWrapper.eq(Question::getCreateTime, new Date());
        dataVo.setTodayTotals(questionMapper.selectCount(queryWrapper));
        // 零赞数
        queryWrapper.clear();
        queryWrapper.eq(Question::getPraise, 0);
        dataVo.setZeroPraise(questionMapper.selectCount(queryWrapper));
        // 今日采纳数
        queryWrapper.clear();
        queryWrapper
                .eq(Question::getUpdateTime, new Date())
                .isNotNull(Question::getAgreeId);
        dataVo.setTodayAgree(questionMapper.selectCount(queryWrapper));
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("askNum", dataVo);
        jsonObject.put("hotAsk", questionMapper.findHotAsk());
        return Result.success(jsonObject);
    }


    /**
     * 获取回答类数据
     * @return
     */
    @Override
    public Result getAnswerCount() {
        DataVo dataVo = new DataVo();
        LambdaQueryWrapper<Answer> queryWrapper = new LambdaQueryWrapper<>();
        // 问题总数
        dataVo.setTotals(answerMapper.selectCount(queryWrapper));
        // 点赞问题总数
        int totalPraise = answerMapper.findAnswerPraiseSum();
        dataVo.setPraiseTotals(totalPraise);
        dataVo.setAvgPraise(totalPraise / dataVo.getTotals());
        // 今日回答数
        queryWrapper.clear();
        queryWrapper.eq(Answer::getCreateTime, new Date());
        dataVo.setTodayTotals(answerMapper.selectCount(queryWrapper));
        // 零赞数
        queryWrapper.clear();
        queryWrapper.eq(Answer::getPraise, 0);
        dataVo.setZeroPraise(answerMapper.selectCount(queryWrapper));
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("answerNum", dataVo);
        jsonObject.put("mvpAnswer", answerMapper.findAnswerMvp());
        jsonObject.put("mvpCount", answerMapper.findMvpCount());
        System.out.println(answerMapper.findAnswerMvp());
        return Result.success(jsonObject);
    }
}
